package com.itheima.task;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import org.apache.poi.ss.usermodel.HorizontalAlignment;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author zhangyan
 * @date 2021/4/15 20:59
 */
public class ExcelWriteDemo2 {
    public static void main(String[] args) {

        WriteCellStyle cellStyle = new WriteCellStyle();
        cellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER); //设置字体居中

        //设置字体样式
        WriteFont font = new WriteFont();
        font.setFontName("宋体");
        font.setFontHeightInPoints((short)12);
        cellStyle.setWriteFont(font);

        HorizontalCellStyleStrategy styleStrategy = new HorizontalCellStyleStrategy(cellStyle, cellStyle);

        //生成Excel报表
        EasyExcel.write("E:\\_374_项目1\\day11\\1.xls")
                .excelType(ExcelTypeEnum.XLS)
                .head(header()) //表头
                .sheet("笑傲江湖")

                .registerWriteHandler(styleStrategy) //设置字体 居中样式
                .registerWriteHandler(new MyColumnStyle()) //设置自定义样式

                .doWrite(data()); //数据行
    }

    //组装表头信息
    private static List<List<String>> header() {
        List<List<String>> headerList = new ArrayList<List<String>>();
        headerList.add(Arrays.asList("编号"));
        headerList.add(Arrays.asList("姓名"));
        headerList.add(Arrays.asList("年龄"));
        headerList.add(Arrays.asList("性别"));
        headerList.add(Arrays.asList("武功"));
        return headerList;
    }

    //组装数据行信息
    private static List<List<String>> data() {
        List<List<String>> dataList = new ArrayList<>();
        dataList.add(Arrays.asList("1","风清扬","88","男","独孤九剑","aaa"));
        dataList.add(Arrays.asList("2","令狐冲","28","男","易筋经"));
        dataList.add(Arrays.asList("3","任盈盈","25","女","吹箫大法"));
        dataList.add(Arrays.asList("4","任我行","60","男","吸星大法"));
        dataList.add(Arrays.asList("5","东方不败","48","男/女","葵花宝典"));
        return dataList;
    }
}
